package cn.com.cloudpioneer.evaluate.service;

import cn.com.cloudpioneer.evaluate.mapper.TargetMapper;
import cn.com.cloudpioneer.evaluate.mapper.TaskMapper;
import cn.com.cloudpioneer.evaluate.model.TargetModel;
import cn.com.cloudpioneer.evaluate.model.TaskModel;
import cn.com.cloudpioneer.evaluate.utils.CustomException;
import cn.com.cloudpioneer.evaluate.utils.Generate;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @auther maruikai
 * @create 2023/9/18-17:11
 */
@Service
public class TargetService {

    @Resource
    private TargetMapper targetMapper;
    @Resource
    private TaskMapper taskMapper;
    @Autowired
    private FileService fileService;

    @Transactional(rollbackFor = Exception.class)
    public Object addTarget(TargetModel targetModel) throws CustomException {
        int flag;
        if (StringUtil.isEmpty(targetModel.getTargetId())) {
            targetModel.setTargetId(Generate.generateId());
            targetModel.setCreateTime(new Date().getTime());
            if (targetModel.getUploadFlag()) {
                if (StringUtil.isEmpty(targetModel.getFileRules())) {
                    throw new CustomException(5005, "文件上传规则未设置");
                }
                targetModel.setFileRules(targetModel.getFileRules() + ".docx");
            }
            flag = targetMapper.insert(targetModel);
        } else {
            flag = targetMapper.update(targetModel);
        }
        if (flag == 0) {
            throw new CustomException(5002, "设置指标失败!");
        }
        return flag;
    }

    public Object setTarget(TargetModel targetModel) throws CustomException {
        if (StringUtil.isEmpty(targetModel.getTargetId())) {
            throw new CustomException(4002, "请求参数错误");
        }
        TargetModel byTargetId = targetMapper.findByTargetId(targetModel.getTargetId());
        if (byTargetId == null) {
            throw new CustomException(5006, "无法找到此指标!");
        }
        if (!targetModel.getUploadFlag()) {
            if (!StringUtil.isEmpty(byTargetId.getFilePath())) {
                fileService.delFile(byTargetId.getFilePath(), 1);
            }
            targetModel.setFilePath(null);
            targetModel.setFileRules(null);
        } else {
            if (!targetModel.getFileRules().equals(byTargetId.getFileRules())) {
                if (!targetModel.getFileRules().contains(".docx")) {
                    targetModel.setFileRules(targetModel.getFileRules() + ".docx");
                }
            }
        }
        Integer update = targetMapper.update(targetModel);
        if (update > 0) {
            this.computeScore(byTargetId.getTaskId());
            TaskModel byId = taskMapper.findById(byTargetId.getTaskId());
            if (!StringUtil.isEmpty(byId.getParentId())) {
                this.computeItemScore(byId.getParentId());
            }
        }
        return update;
    }

    public Object targetId(String targetId) throws CustomException {
        if (StringUtil.isEmpty(targetId)) {
            throw new CustomException(4002, "请求参数错误");
        }
        return targetMapper.delTarget(true, targetId);

    }

    public Object getTargetList(String taskId) {
        List<TargetModel> byTaskId = targetMapper.findByTaskId(taskId);
        for (TargetModel targetModel : byTaskId) {
            if (!StringUtil.isEmpty(targetModel.getFilePath())) {
                String previewUrl = fileService.formatViewUrl(targetModel.getFilePath());
                targetModel.setPreviewUrl(previewUrl);
                targetModel.setFileName(targetModel.getFileRules());
            }
        }
        return byTaskId;
    }

    private void computeScore(String taskId) {
        List<TargetModel> byTaskId = targetMapper.findByTaskId(taskId);
        if (byTaskId.size() == 0) return;
        int score = 0;
        for (TargetModel targetModel : byTaskId) {
            if (targetModel.getScore() != 0) {
                score += targetModel.getScore();
            }
        }
        taskMapper.updateScore(score / byTaskId.size(), taskId);
    }

    public void computeItemScore(String parentTaskId) {
        List<TaskModel> byParentIt = taskMapper.findByParentIt(parentTaskId);
        if (byParentIt.size() == 0) return;
        int score = 0;
        for (TaskModel taskModel : byParentIt) {
            if (taskModel.getScore() != 0) {
                score += taskModel.getScore();
            }
        }
        taskMapper.updateScore(score / byParentIt.size(), parentTaskId);
    }
}
